package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.api.client.json.Json;
import org.jeecg.modules.gwcjava.jiekou.entity.CategoryInfo;
import org.jeecg.modules.gwcjava.jiekou.service.BusinessLogicService;
import org.jeecg.modules.system.entity.AgreementEntity;
import org.jeecg.modules.system.service.AgreementService;
import org.jeecg.modules.zonglzp.entity.ProductEntity;
import org.jeecg.modules.zonglzp.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    AgreementService agreementservice;

    @Autowired
    BusinessLogicService logicService;

    @Autowired
    ProductService service;

    /*
     查询产品
     */
    @GetMapping("/getProductlist")
    public JSONObject getProductlist(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            String productCode, //产品编码
            @RequestParam(name = "productType", defaultValue = "10") int productType, //产品类型
            @RequestParam(name = "sex", defaultValue = "10") int sex
    ) {

        JSONObject obj = new JSONObject();
        try {
            Page<ProductEntity> page = new Page<ProductEntity>(pageNo, pageSize);
            IPage<ProductEntity> pageList = service.getProductlist(page, productCode, productType, sex);
            if (pageList == null) {
                obj.put("code", 1);
                obj.put("data", "未查到数据");
            } else {
                obj.put("code", 0);
                obj.put("data", pageList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }


    /*
    查询产品 反list的
     */
    @GetMapping("/getxianshuoproductList")
    public JSONObject getxianshuoproductList() {
        JSONObject obj = new JSONObject();
        List<ProductEntity> list = service.getxianshuoproductList();
        if (list == null) {
            obj.put("code", 1);
            obj.put("data", "未查到数据");
        } else {
            obj.put("code", 0);
            obj.put("data", list);
        }
        return obj;
    }


    /*
    产品添加到部门
     */
    @PostMapping("/addProductToDepart")
    public JSONObject addProductToDepart(
            @RequestParam(name = "productId") String productId,   //产品id
            @RequestParam(name = "customerDepartId") String customerDepartId,  //客户部门id
            String remark,   //备注信息
            String xiuziOneNeirong, //绣字第一行内容
            String xiuziTwoNeirong,  //袖子第二行内容
            String xiuziWeizhi, //绣字位置
            String xiuziColor, // 绣字颜色
            String xiuziZiti,  //绣字字体
            String fabricId, //面料id
            @RequestParam(name = "numbers", defaultValue = "1") int numbers
    ) {
        JSONObject obj = new JSONObject();


        //查询是否是公司 不然的话就拒绝
        int isconpangy = service.getIsconpangy(customerDepartId);
        if (isconpangy != 1) {
            obj.put("code", 1);
            obj.put("data", "不是公司禁止添加");
            return obj;
        }

        if (xiuziOneNeirong != null || xiuziTwoNeirong != null || xiuziWeizhi != null || xiuziColor != null || xiuziZiti != null || fabricId != null) {
            //下面的操作是如果选了其他的属性，就需要修改产品的信息
            service.updateProduct(fabricId, xiuziZiti, xiuziColor, xiuziWeizhi, xiuziTwoNeirong, xiuziOneNeirong, productId);
        }


        //查询是否存在产品之前
        String str = service.queryproductdepart(productId, customerDepartId);
        if (str != null) {
            //查询该部门该产品的件数是否有
            int productNumber = service.queryproductNumber(productId, customerDepartId);
            service.updateNumber(productId, customerDepartId, (productNumber + numbers));
            if (productNumber != 0) {
                obj.put("code", 0);
                obj.put("data", "添加成功");
                return obj;
            }
        }

        //将产品id和部门id查到产品客户部门表中
        int count = service.addProductToDepart(remark, productId, customerDepartId, numbers);
        if (count > 0) {
            obj.put("code", 0);
            obj.put("data", "添加成功");
        } else {
            obj.put("code", 1);
            obj.put("data", "添加失败");
        }
        return obj;
    }


    /*
       部门下的产品列表查询
     */
    @GetMapping("/getdepartProductList")
    public JSONObject departProductList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "departId") String departId
    ) {
        JSONObject obj = new JSONObject();

        Page<ProductEntity> page = new Page<ProductEntity>(pageNo, pageSize);
        IPage<ProductEntity> pageList = service.getDepartProductlist(page, departId);
        if (pageList.getRecords().size() == 0) {
            obj.put("code", 1);
            obj.put("data", "未查到数据");
        } else {
            obj.put("code", 0);
            obj.put("data", pageList);
        }
        return obj;
    }



    /**
     * 返回品类的id过去
     */
    @GetMapping("/getcateId")
    public JSONObject getcateId(
            String ids
    ){
        JSONObject obj=new JSONObject();
        String categoryids[] = ids.split(",");
        String pinglei_id = "";
        for (String s:categoryids){
            CategoryInfo categoryInfo = logicService.query_categoryDetailInfo(s);
            if(categoryInfo!=null){
                if(categoryInfo.getIsType()==2){
                    pinglei_id = categoryInfo.getId().toString();
                    break;
                }
            }
        }

        AgreementEntity entitycunzai=agreementservice.getcanzhaobycateId(pinglei_id);
        if(entitycunzai==null)
            entitycunzai=agreementservice.getAgreementEntitybyId(1);
        obj.put("data",entitycunzai);
        return obj;
    }

}
